<?php

namespace App\Http\Middleware;

use App\Events\HomeVisitEvent;
use App\Master\Home\CacheSystem;
use App\Utils\LogUtil;
use Closure;
use Illuminate\Http\Request;

class HomeAgent
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        //日志统一写入
        register_shutdown_function([new LogUtil, 'close']);
        LogUtil::getInstance('Home/'); //设置日志存入通道

        $path = $request->path();

        $params = [
            'path'  => $path,
//            'menus' => CacheSystem::createMenus($path),
            'site_info' => CacheSystem::createSite(),
        ];
//        dd($params);
        view()->share($params);

        $response = $next($request);

//        $requestUri = $path;// 当前请求地址
//        if (!in_array($requestUri,self::whiteList())){
//            (new HomeVisitEvent());
//        }

        return $response;
    }

    //跳转404的链接
    private function url404()
    {
        return [];
    }

    //白名单
    public function whiteList()
    {
        return [];
    }
}
